package com.lab.thread;   

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

import com.lab.dto.DataView;
import com.lab.dto.ExecuteAction;
import com.lab.pojo.Event;
import com.lab.pojo.EventDAO;
import com.lab.pojo.MonitorDAO;
import com.lab.pojo.MonitorpropertyDAO;
import com.lab.pojo.Sensor;
import com.lab.pojo.SensorDAO;
import com.lab.pojo.Sensorproperty;
import com.lab.pojo.SensorpropertyDAO;
import com.lab.pojo.Tactics;
import com.lab.pojo.TacticsDAO;
import com.lab.pojo.User;
import com.lab.pojo.UserDAO;
import com.lab.dto.ExecuteActionPool;
/**
 * @author wuhao
 * @version 创建时间：Apr 18, 2013 9:02:40 PM
 * 类说明
 */
public class TacticThread implements Runnable {
	
	
	@Autowired
	//注解注入USER，从Session获取。
	private  User user;
	

	WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext();
	MonitorDAO monitorDao = (MonitorDAO) context.getBean("MonitorDAO");
	MonitorpropertyDAO monitorpropertyDAO = (MonitorpropertyDAO)context.getBean("MonitorpropertyDAO");
	EventDAO eventDAO = (EventDAO)context.getBean("EventDAO");
	SensorDAO sensorDAO = (SensorDAO)context.getBean("SensorDAO");
	SensorpropertyDAO sensorpropertyDAO = (SensorpropertyDAO)context.getBean("SensorpropertyDAO");
	TacticsDAO tacticsDAO = (TacticsDAO)context.getBean("SensorpropertyDAO");
	UserDAO userDAO = (UserDAO)context.getBean("userDAO");
	
	
	//存储event
	public void saveEvent(Sensor sensor,Sensorproperty sensorproperty,Tactics tactics){
		Event event = new Event();
		event.setSensorName(sensor.getSensorName());
		event.setSensorPropertyName(sensorproperty.getSensorPropertyName());
		event.setTacticsId(tactics.getId());
		event.setTriggerTime(new Date());
		event.setEventStatus("未处理");
		eventDAO.save(event);
	}
	
	public void executeAction(Tactics tactics){
		ExecuteAction executeAction = new ExecuteAction();
		executeAction.setActionName(tactics.getActuatorName());
		executeAction.setActionPropertyName(tactics.getActuatorPropertyName());
		executeAction.setActionPropertyValue(tactics.getActuatorPropertyValue());
		executeAction.setDelayTime(tactics.getDelayTime());
		executeAction.setIssms(tactics.getIssms());
		executeAction.setStatus(tactics.getStatus());
		ExecuteActionPool.executeActionPool.add(executeAction);
		
	}
	
	
	@SuppressWarnings("unchecked")
	public void run() {
		while(true){
			try {
				Thread.sleep(500);
				List<Tactics> tacticsList = tacticsDAO.findAll();
				for(Tactics tacticsItem : tacticsList){
					Sensorproperty sensorproperty =  sensorpropertyDAO.findById(tacticsItem.getSensorPropertyId());
					Sensor sensor = sensorDAO.findById(sensorproperty.getSensorId());
					Float compareValue = Float.parseFloat(DataView.get(sensorproperty.getId()).getValue());
					
					if(tacticsItem.getCondition().equals("==")){
						if(compareValue.floatValue() == Float.parseFloat(tacticsItem.getSensorPropertyValue())){
							saveEvent(sensor, sensorproperty, tacticsItem);
							executeAction(tacticsItem);
						}
					}else if(tacticsItem.getCondition().equals(">=")){
						if(compareValue.floatValue() >= Float.parseFloat(tacticsItem.getSensorPropertyValue())){
							saveEvent(sensor, sensorproperty, tacticsItem);
							executeAction(tacticsItem);
						}
					}else if(tacticsItem.getCondition().equals(">")){
						if(compareValue.floatValue() > Float.parseFloat(tacticsItem.getSensorPropertyValue())){
							saveEvent(sensor, sensorproperty, tacticsItem);
							executeAction(tacticsItem);
						}
					}else if(tacticsItem.getCondition().equals("<=")){
						if(compareValue.floatValue() <= Float.parseFloat(tacticsItem.getSensorPropertyValue())){
							saveEvent(sensor, sensorproperty, tacticsItem);
							executeAction(tacticsItem);
						}
					}else if(tacticsItem.getCondition().equals("<")){
						if(compareValue.floatValue() < Float.parseFloat(tacticsItem.getSensorPropertyValue())){
							saveEvent(sensor, sensorproperty, tacticsItem);
							executeAction(tacticsItem);
						}
					}else if(tacticsItem.getCondition().equals("!=")){
						if(compareValue.floatValue() != Float.parseFloat(tacticsItem.getSensorPropertyValue())){
							saveEvent(sensor, sensorproperty, tacticsItem);
							executeAction(tacticsItem);
						}
					}
				}
			} catch (Exception e) {
				
			}
		}
	}

}
  